#pragma once

#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAddr
{
public:
    InetAddr(struct sockaddr_in& addr)
    :_addr(addr)
    {
        // 网络 -> 主机
        char buf[32];
        inet_ntop(AF_INET,&_addr.sin_addr,buf,sizeof(buf)-1);
        _ip = buf;

        _port = ntohs(_addr.sin_port);
    }

    InetAddr(std::string& ip,uint16_t port)
    :_ip(ip)
    ,_port(port)
    {
        // 主机 -> 网络
        inet_pton(AF_INET,_ip.c_str(),&_addr.sin_addr);

        _addr.sin_port = htons(_port);
    }

    std::string Ip() const
    {
        return _ip;
    }

    uint16_t Port() const
    {
        return _port;
    }
    
private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};